﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Metier;
using Poco;
using WebApp.Models;

namespace WebApp.Controllers
{
    [Authorize(Roles="Serveur")]
    public class ServeurController : Controller
    {
        //
        // GET: /Serveur/

        public ServeurController()
            : base()
        {
            ViewBag.Home = "/Serveur/";
        }

        public ActionResult Index(string email)
        {

            
            return View();
        }

        public ActionResult Tables(string email)
        {
            GestionZone manager_zone = new GestionZone();            
            GestionTable manager_table = new GestionTable();
            Zone serverzone = manager_zone.Chercher(User.Identity.Name);
            List<Table> list_table = manager_table.Lister(serverzone);

            ServerModelView servermodelview = new ServerModelView(User.Identity.Name);
            servermodelview.Tables = list_table;
            

            return View("ListTables", servermodelview);
        }

        public ActionResult Notifications()
        {
            GestionNotification manager_notif = new GestionNotification();
 
            //Zone serverzone = manager_zone.Chercher(User.Identity.Name);
            GestionCommande manager_commandes = new GestionCommande();
            
            //TypeNotification.AppelServeur;
            ServerModelView servermodelview = new ServerModelView(User.Identity.Name);
            servermodelview.Notifications = manager_notif.Lister(User.Identity.Name, false);
            
            return View("ListNotifs", servermodelview);
        }
        
        public ActionResult ValidNotif(int id)
        {
            GestionNotification mana_notif = new GestionNotification();
            try{
                mana_notif.MarquerCommeFait(id);
            }catch(Exception e){
                FlashHelpers.FlashError(this, "Une erreur est survenue pendant la validatino de la notification");
            }
            FlashHelpers.FlashSuccess(this, "Notification validé avec succés");
            return RedirectToAction("Notifications");
        }
        
        public ActionResult HomeWithServeur()
        {
            List<Table> list_table = new List<Table>();

            try
            {
                GestionServeur mana_serveur = new GestionServeur();
                GestionTable mana_table = new GestionTable();

                //Looking for a server
                Serveur serveur = mana_serveur.Chercher(User.Identity.Name);
                    
                list_table = mana_table.Lister(serveur.Zone);

                ViewBag.tables = list_table;

                return View();
            }
            catch (Exception e)
            {
                FlashHelpers.FlashError(this, "Vous n'avez pas accès à cette page");
                RedirectToAction("Index");
            }
       
            return View(list_table);
        }

        //[HttpPost]
        //public ActionResult HomeWithServeur (Table table)
        //{
        //    return Re
        //}
    }
}
